import { _decorator, Component, Node, v2, v3 } from 'cc';
import { GameMgrShi } from './GameMgrShi';
import { getDistance, randomNum, trackTarget } from '../script/utils/utils';
const { ccclass, property } = _decorator;

@ccclass('Biont')
export class Biont extends Component {

    speedMove=130;
    targetPos=v3();

    biontType;


    start() {
        this.targetPos = this.getNextTargetPos();
    }

    protected update(dt: number): void {
        if (GameMgrShi.ins.isGameIng) {
            if (this.targetPos) {
                this.biontTrackTargetMove(this.targetPos, this.node, this.speedMove, dt);
            }
          
        }
       
    }

    tempPos = v3();

    init(_biontType) {
        this.biontType=_biontType;
    }

    biontTrackTargetMove(targetPos, selfNode, speed, dt) {

        if (!targetPos) {
            return
        }

        let speedArr = trackTarget(targetPos.x, targetPos.y, selfNode.position.x, selfNode.position.y, speed * dt);

        selfNode.getPosition(this.tempPos);
        this.tempPos.x += speedArr[0]
        this.tempPos.y += speedArr[1]
        selfNode.setPosition(this.tempPos);

        selfNode.angle = speedArr[2] * (180 / Math.PI);


        //如果自己已经到达附近
        if (getDistance(selfNode.position.x, selfNode.position.y, targetPos.x, targetPos.y) < 50) {
            let nextTartgetPos = this.getNextTargetPos();
            this.targetPos = nextTartgetPos;
        }
    }

    getNextTargetPos() {
        let res = null;
        while (!res) {
            // if (this.biontType <= 6) {
            //     let tempRes = v2(this.node.position.x + gameMgr.ins.randowNum(-2000, 2000), this.node.position.y + gameMgr.ins.randowNum(-2000, 2000))
            //     if (Math.abs(tempRes.x)>gameMgr.ins.mapBoundary.x||Math.abs(tempRes.x)>gameMgr.ins.mapBoundary.y) {
            //         continue;
            //     }
            //     else{
            //         res=tempRes;
            //     }
            // }
            // else {
            let tempRes = v2(this.node.position.x + randomNum(-1000, 1000), this.node.position.y + randomNum(-1000, 1000))
            if (Math.abs(tempRes.x) > GameMgrShi.ins.mapBoundary.x || Math.abs(tempRes.y) > GameMgrShi.ins.mapBoundary.y) {
                continue;
            }
            else {
                res = tempRes;
            }
            // }

            return res;
        }

    }

   
}


